﻿using Microsoft.AspNetCore.Mvc;
using PetaPoco;
using Qy.Common;
using Qy.Models;
using Qy.Service;
using Qy.Web.Authorize;
using System.Threading.Tasks;

namespace Qy.Web.Api
{
    [Route("Api/[controller]/[action]")]
    [Verify]
    public class LogController : Base
    {
        private readonly LogService _logService;
        private readonly LogLoginService _logLoginService;
        private readonly LoginUtil _LoginUtil;
        public LogController(LoginUtil LoginUtil, LogService logService, LogLoginService logLoginService)
        {
            _LoginUtil = LoginUtil;
            _logService = logService;
            _logLoginService = logLoginService;
        }

        #region 数据库操作日志
        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Log_Index)]
        public async Task<IActionResult> IndexAsync(int page = 1, int limit = 15)
        {
            int userId = _LoginUtil.LoginUserId();
            int groupId = _LoginUtil.LoginGroupId();
            int adminUid = groupId != 1 ? userId : 0;//超级管理员显示全部数据
            Page<ViewLogSql> list = await _logService.GetSqlListAsync(page, limit, adminUid);
            return Json(new { status = 1, data = list.Items, count = list.TotalItems });
        }

        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Log_Index)]
        public async Task<IActionResult> IndexLoginAsync(int page = 1, int limit = 15)
        {
            int userId = _LoginUtil.LoginUserId();
            int groupId = _LoginUtil.LoginGroupId();
            int adminUid = groupId != 1 ? userId : 0;//超级管理员显示全部数据
            Page<ViewLogLogin> list = await _logLoginService.GetSqlListAsync(page, limit, adminUid);
            return Json(new { status = 1, data = list.Items, count = list.TotalItems });

        }

        #endregion


        #region 登录日志

        [HttpDelete]
        [ActionPermissionFilter(Permission = PermissionKeys.Log_Clear)]
        [Log(Name = "清除日志")]
        public async Task<IActionResult> Clear()
        {
            await _logService.Clear();
            return Json(new { status = 1, msg = "清除数据库操作日志成功！" });
        }




        [HttpDelete]
        [ActionPermissionFilter(Permission = PermissionKeys.Log_Clear)]
        [Log(Name = "清除登录日志")]
        public async Task<IActionResult> ClearLogin()
        {
            await _logLoginService.Clear();
            return Json(new { status = 1, msg = "清除登录日志成功！" });

        }

        #endregion
    }
}
